from sklearn.feature_extraction import DictVectorizer
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
import pandas as pd
"""
字典特征抽取
"""
data = [{'city': '北京', 'temperature': 100},
        {'city': '上海', 'temperature': 60},
        {'city': '深圳', 'temperature': 30}]

# 1、实例化一个转换器类
# transfer = DictVectorizer()   # sparse=False 意味着转换结果将不是稀疏矩阵，而是一个常规的密集数组
transfer = DictVectorizer(sparse=False)

# 2、调用fit_transform()          字典或者包含字典的迭代器，返回值sparse矩阵
data_new = transfer.fit_transform(data)
print("data_new：\n", data_new)

# 3、调用inverse_transform()       array数组或者sparse矩阵，返回值转换之前的数据格式
data_real = transfer.inverse_transform(data_new)
print("data_real: \n", data_real)

print("特征名字：\n", transfer.get_feature_names_out())


# 1、获取数据
data = pd.read_csv("datingTestSet2.txt", sep='\t')

# 取原始数据中的所有行，前三列数据
data = data.iloc[:, :3]
print("data:\n", data)

# 2、实例化一个转换器类
transform = MinMaxScaler()

# 3、调用fit_transform
data_new = transform.fit_transform(data)
print("data_new:\n", data_new)

# 1、获取数据
data = pd.read_csv("datingTestSet2.txt", sep='\t')

# 取原始数据中的所有行，前三列数据
data = data.iloc[:, :3]
print("data:\n", data)

# 2、实例化一个转换器类
transform = StandardScaler()

# 3、调用fit_transform
data_new = transform.fit_transform(data)
print("data_new:\n", data_new)
